package com.example.shopping2.a_c_fragment.car

import BaseFragment
import IFragment
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.car.adapter.CarList_Adapter
import com.example.shopping2.a_c_fragment.car.entity.CarList_Entity
import com.example.shopping2.a_c_fragment.car.entity.SubCarEntity
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.LjlUtils


class CarFragment : BaseFragment<HomePresenter>(), IFragment, HomeContract.HomeView {
    private val carFragmentBj: TextView by lazy { findViewById<TextView>(R.id.car_fragment_bj) }
    private val carFragmentPay: AppCompatButton by lazy { findViewById<AppCompatButton>(R.id.car_fragment_pay) }
    private val carFragmentAll: CheckBox by lazy { findViewById<CheckBox>(R.id.car_fragment_all) }
    private val carFragmentTxt: TextView by lazy { findViewById<TextView>(R.id.car_fragment_txt) }
    private val carFragmentRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.car_fragment_rec) }
    private var userid: Long? = 0
    private var sum = 00.00
    private var floag = 0
    var mybro = MyBro()

    override fun findlayout(): Int {
        return R.layout.fragment_car
    }

    override fun initView() {
        carFragmentTxt.setText("总价：00.00")
        userid =
            activity?.getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE)
                ?.getLong("id", 1)
        //注册广播
        var intentfilter = IntentFilter()
        intentfilter.addAction("user_login")
        activity?.registerReceiver(mybro, intentfilter)

    }


    override fun initData() {

        getdata()

    }

    fun getdata() {
        mypre = HomePresenter(HomeModel(), this)
        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        var map: HashMap<String, Any> = HashMap()

        map.put("id", userid!!)
        map.put("code", HttpCode.CARLIST)
        list.add(map)
        mypre.requestAll(list)
    }

    override fun onStart() {
        super.onStart()
        floag = 0
        sum = 00.00
        carFragmentTxt.setText("总价：" + sum)
        carFragmentAll.isChecked = false
        getdata()

    }

    override fun showHomeData(baseEntity: BaseEntity) {
        if (baseEntity is CarList_Entity) {
            var data = baseEntity.data
            if (data == null){
                return
            }
            var adapter = CarList_Adapter(data as ArrayList<CarList_Entity.DataBean>)
            carFragmentRec.adapter = adapter
            carFragmentRec.layoutManager = LinearLayoutManager(context)
            adapter.addChildClickViewIds(
                R.id.car_item_radio,
                R.id.car_item_left,
                R.id.car_item_right
            )
            //-----------------------------------------适配器点击-------------------------------------
            adapter.setOnItemChildClickListener { adapter, view, position ->
                when (view.id) {
                    R.id.car_item_radio -> {
                        if (data[position].selFlag) {
                            //取消选中
                            floag--
                            var goodsPrice = data[position].goods_price
                            var goodsCount = data[position].goods_count
                            sum -= (goodsPrice * goodsCount).toDouble()
                            carFragmentTxt.setText("总价：" + sum)
                            data[position].selFlag = false
                            carFragmentAll.isChecked = false
                        } else {
                            //选中
                            floag++
                            var goodsPrice = data[position].goods_price
                            var goodsCount = data[position].goods_count
                            sum += (goodsPrice * goodsCount).toDouble()
                            carFragmentTxt.setText("总价：" + sum)
                            data[position].selFlag = true
                            if (floag == data.size) {
                                carFragmentAll.isChecked = true
                            }
                        }


                    }
                    R.id.car_item_left -> {

                        var goodsCount = data[position].goods_count
                        if (goodsCount == 1) {

                        } else {
                            --data[position].goods_count
                            if (data[position].selFlag) {
                                var goodsPrice = data[position].goods_price

                                sum -= goodsPrice
                                carFragmentTxt.setText("总价：" + sum)
                            }
                        }

                    }
                    R.id.car_item_right -> {

                        ++data[position].goods_count
                        if (data[position].selFlag) {
                            var goodsPrice = data[position].goods_price

                            sum += goodsPrice
                            carFragmentTxt.setText("总价：" + sum)
                        }

                    }
                }
                adapter.notifyDataSetChanged()

            }

            //-----------------------------------------全选按钮-------------------------------------
            carFragmentAll.setOnClickListener {

                if (carFragmentAll.isChecked) {
                    floag = data.size-1
                    for (i in 0..data.size - 1) {
                        if (data[i].selFlag) {

                        } else {
                            var goodsCount = data[i].goods_count
                            var goodsPrice = data[i].goods_price
                            sum += (goodsCount * goodsPrice).toDouble()
                            carFragmentTxt.setText("总价：" + sum)
                            data[i].selFlag = true
                        }

                    }
                } else {
                    floag = 0

                    for (i in 0..data.size - 1) {
                        data[i].selFlag = false
                        var goodsCount = data[i].goods_count
                        var goodsPrice = data[i].goods_price
                        sum -= (goodsCount * goodsPrice).toDouble()
                        carFragmentTxt.setText("总价：" + sum)
                    }
                }
                adapter.notifyDataSetChanged()
            }

            //-----------------------------------------编辑按钮的逻辑-------------------------------------
            carFragmentBj.setOnClickListener {

                if (data.get(0).edtFlag) {
                    carFragmentBj.setText("编辑")
                    carFragmentPay.setText("结算")
                    data.get(0).edtFlag = false
                } else {
                    carFragmentBj.setText("完成")
                    carFragmentPay.setText("删除")
                    data.get(0).edtFlag = true
                }
                adapter.setNewInstance(data)
            }
            //-----------------------------------------结算按钮-------------------------------------
            carFragmentPay.setOnClickListener {
                if (data.get(0).edtFlag) {
                    LjlUtils.Ljl_Loge("删除")


                } else {
                    LjlUtils.Ljl_Loge("结算")
                    if (data.size == 0) {
                        LjlUtils.Ljl_Toast(activity!!, "购物车没数据")
                    }
                    if (floag == 0) {
                        LjlUtils.Ljl_Toast(activity!!, "请选择商品")
                    }
                    var goods: ArrayList<CarList_Entity.DataBean> = ArrayList()
                    for (good in data) {
                        if (good.selFlag) {
                            goods.add(good)
                        }
                    }

                    var list: ArrayList<HashMap<String, Any>> = ArrayList()
                    var map: HashMap<String, Any> = HashMap()
                    map.put("code",HttpCode.COMMITCAR)
                    map.put("goodsList",goods)
                    map.put("totalPrice", sum)
                    map.put("userId", userid!!)
                    list.add(map)
                    mypre.requestAll(list)
                }
            }


        }else if(baseEntity is SubCarEntity){
            if (baseEntity.status.equals("0")){

                if (floag == 0) {
                    LjlUtils.Ljl_Toast(activity!!, "请选择商品")
                }else{
                    var intent = Intent(activity!!,ComOrderActivity::class.java)
                    intent.putExtra("data",baseEntity.data)

                    startActivity(intent)
                }

            }


        }

    }


    //-----------------------------------------广播接收者-------------------------------------
    inner class MyBro : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == "user_login") {

                userid =
                    activity?.getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE)
                        ?.getLong("id", 1)

                getdata()

            }
        }

    }


}